<?php
require_once("./vo/com/artxis/cms/vo/MenusVO.php");
require_once("config.php");
class MenusService {
	function MenusService()

	{
			//connect to the database.
			$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
			mysql_select_db(DATABASE_NAME);
	}
	
  function bolean($bolean){
	  if($bolean == "1"){
	  $bool = "true";
	  }else{
	  $bool = "false" ;
	  }
  return $bool;
  }

  function doSerchChildMenu($idmenux){
  $arrChild = array();
  $sql = mysql_query("SELECT*FROM menus WHERE parentId = '".$idmenux."' AND visible='1' ORDER BY orderId ASC");
 
		 while($tableDatax = mysql_fetch_array($sql)){
		  $dataChild = $this->doSerchChildMenu($tableDatax["idmenu"]);
					 $menusVO = new MenusVO();
					 $menusVO->idmenu   = $tableDatax["idmenu"];
					 $menusVO->label    = $tableDatax["menuName"];
					 $menusVO->tolltip  = $tableDatax["description"];
					 $menusVO->visible  = $this->bolean($tableDatax["visible"]);
					 $menusVO->enabled  = $this->bolean($tableDatax["enable"]);
					 $menusVO->type     = $tableData["type"];
					 $menusVO->data     = $tableData["idmenu"];
						 // Do Search Child Menu
						
						if($dataChild != array()){
						  $menusVO->children = $dataChild;
						}
						 
						 $arrChild[] = $menusVO;
		}
  return $arrChild;
  }

	function checkChild($idparent){
		//$ambildata = mysql_query("SELECT*FROM " . TABLE_PREFIX . "adminpages  order by idAdminpages");
		$sql =  mysql_query("SELECT*FROM menus WHERE parentId = '$idparent' ORDER BY idmenu ASC");
		while($lihatdb = mysql_fetch_array($sql)){
			if($lihatdb["parentId"]==$idparent && $lihatdb["type"]=="itm" ){
				
				$menusChild.='<children idmenu ="'.$lihatdb["idmenu"].'" visible ="'.$this->bolean($lihatdb["visible"]).'" type ="'.$lihatdb["type"].'" label ="'.$lihatdb["menuName"].'" ModulePath ="'.$lihatdb["ModulePath"].'" kategoriName ="'.$lihatdb["kategoriName"].'" openMethod ="'.$lihatdb["openMethod"].'"/>';
			}
			
			if($lihatdb["parentId"]==$idparent && $lihatdb["type"]=="cat" ){
				$menusChild.='<children idmenu ="'.$lihatdb["idmenu"].'" visible ="'.$this->bolean($lihatdb["visible"]).'" type ="'.$lihatdb["type"].'" label ="'.$lihatdb["menuName"].'" ModulePath ="'.$lihatdb["ModulePath"].'" kategoriName ="'.$lihatdb["kategoriName"].'" openMethod ="'.$lihatdb["openMethod"].'">';
				$menusChild.=$this->checkChild($lihatdb["idmenu"]);
				$menusChild.="</children>";
			}
		}
		return $menusChild;
	}
  
  public function GetMenus()
    {	

	$sql =  mysql_query("SELECT*FROM menus WHERE parentId = '0'  AND visible='1' ORDER BY orderId ASC");
	
	$arrRootMenu = array();
	$menus.="<?xml version='1.0' encoding='ISO-8859-1' ?>";
	
	while($tableData = mysql_fetch_array($sql)){
		

/*		 $menusVO = new MenusVO();
		 $menusVO->idmenu  = $tableData["idmenu"];
		 $menusVO->label   = $tableData["menuName"];
		 $menusVO->tolltip = $tableData["description"];
		 $menusVO->visible = $this->bolean($tableData["visible"]);
		 $menusVO->enabled = $this->bolean($tableData["enable"]);
		 $menusVO->type    = $tableData["type"];
		 $menusVO->data    = $tableData["idmenu"];*/
		     // Do Search Child Menu
			 if($tableData["parentId"]==0 && $tableData["type"]=="cat"){
	
	             $menus.='<menuitem idmenu ="'.$tableData["idmenu"].'" visible ="'.$this->bolean($tableData["visible"]).'" type ="'.$tableData["type"].'" label ="'.$tableData["menuName"].'" ModulePath ="'.$tableData["ModulePath"].'" kategoriName ="'.$tableData["kategoriName"].'" openMethod ="'.$tableData["openMethod"].'">';
			     $menus.=$this->checkChild($tableData["idmenu"]);
	             $menus.="</menuitem>";
			 }else if($tableData["parentId"]==0 && $tableData["type"]=="itm"){
				 $menus.='<menuitem idmenu ="'.$tableData["idmenu"].'" visible ="'.$this->bolean($tableData["visible"]).'" type ="'.$tableData["type"].'" label ="'.$tableData["menuName"].'" ModulePath ="'.$tableData["ModulePath"].'" kategoriName ="'.$tableData["kategoriName"].'" openMethod ="'.$tableData["openMethod"].'"/>';
			 }
		 }
	   
		return $menus;


    }

}

?>
